//
//  ViewController.swift
//  LoginDemo
//
//  Created by 王峰 on 2020/10/14.
//  Copyright © 2020 王峰. All rights reserved.
//

import UIKit
import ATAuthSDK
class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let loginBtn = UIButton()
        
        loginBtn.frame = .init(x: self.view.frame.size.width/2-100, y: 200, width: 200, height: 40);
        loginBtn.backgroundColor = UIColor.blue
        loginBtn.setTitle("拉起登录", for: .normal) 
        loginBtn.setTitleColor(UIColor.red, for: .normal)
        loginBtn.addTarget(self, action: #selector(loginAction), for:.touchUpInside)
        self.view.addSubview(loginBtn)
        
        
    }
    
    
    @objc func loginAction(){
        
        let tXCustomModel = TXCustomModel()
        
        tXCustomModel.presentDirection = PNSPresentationDirection.right
        
        tXCustomModel.navColor = UIColor.white
        
        let str = ""
        
        let attributeString = NSMutableAttributedString(string:"")
        
        attributeString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.white,range: NSMakeRange(0, str.count))
        
        attributeString.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "HelveticaNeue-Bold", size: 14)!,range: NSMakeRange(0,str.count))
        
        tXCustomModel.navTitle = attributeString
        
        tXCustomModel.navBackImage = UIImage(named: "nav_black_close")!
        
//        tXCustomModel.logoImage = UIImage(named: "about-1")!
        
        tXCustomModel.changeBtnIsHidden = false;//其他登录方式
        
        let str1 = "验证码或密码登录"
        
        let attributeString1 = NSMutableAttributedString(string:str1)
        
        attributeString1.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red,range: NSMakeRange(0, str1.count))
        
        attributeString1.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "HelveticaNeue-Bold", size: 14)!,range: NSMakeRange(0,str1.count))
        
        tXCustomModel.changeBtnTitle = attributeString1
        
        tXCustomModel.sloganIsHidden = true;
        
        tXCustomModel.loginBtnText = attributeString
        
        tXCustomModel.loginBtnBgImgs = [UIImage(named: "yjdl")!,UIImage(named: "yjdl")!,UIImage(named: "yjdl")!]
        
        tXCustomModel.checkBoxIsChecked = true
        
        tXCustomModel.checkBoxIsHidden = true
        
        tXCustomModel.privacyPreText = "使用即为同意"
        
        tXCustomModel.privacyOperatorPreText = "《"
        
        tXCustomModel.privacyOperatorSufText = "》"
        
        tXCustomModel.privacyOne = ["《用户服务协议》", ""]
        
        tXCustomModel.privacyTwo = ["《隐私政策》", ""]
        
        tXCustomModel.privacyNavBackImage = UIImage(named: "nav_black_close")!
        
        
        
        TXCommonHandler.sharedInstance().getLoginToken(withTimeout: 5.0, controller: self, model: tXCustomModel) { (resultDic) in
            
            
            let resultCode: String = resultDic["resultCode"] as! String
            
            if PNSCodeLoginControllerPresentSuccess == resultCode{
                
                print("授权页拉起成功回调")
            }else if PNSCodeLoginControllerClickCancel == resultCode || PNSCodeLoginControllerClickChangeBtn == resultCode || PNSCodeLoginControllerClickLoginBtn == resultCode || PNSCodeLoginControllerClickCheckBoxBtn == resultCode || PNSCodeLoginControllerClickProtocol == resultCode{
                print("页面点击事件回调")
                
                if PNSCodeLoginControllerClickChangeBtn == resultCode {//验证码或密码登录
                    
                    
                }
            }else if PNSCodeSuccess == resultCode {
                print("获取LoginToken成功回调,接下来可以拿着Token去服务端换取手机号，有了手机号就可以登录，SDK提供服务到此结束")
                
                let token: String = resultDic["token"] as! String
                
                TXCommonHandler.sharedInstance().cancelLoginVC(animated: true, complete: nil)
                
            }else{
                print("获取LoginToken或拉起授权页失败回调")
                
            }
        }
    }
    
}




